using System.Windows.Forms;
using System.ComponentModel;
using PluginInterface;
using System;
using System.Drawing;
using Tinke;
using MKDS_Course_Editor.Properties;
namespace Tinke
{
public class iNCGR : UserControl
{
    // Fields
    private Button btnSave;
    private ColumnHeader columnCampo;
    private ColumnHeader columnPos;
    private ColumnHeader columnValor;
    private ComboBox comboBox1;
    private ComboBox comboBox2;
    private ComboBox comboDepth;
    private IContainer components;
    private GroupBox groupProp;
    private Label label1;
    private Label label2;
    private Label label3;
    private Label label4;
    private Label label5;
    private Label label6;
    private Label label7;
    private Label label8;
    private ListView listInfo;
    private NumericUpDown numericHeight;
    private NumericUpDown numericStart;
    private NumericUpDown numericWidth;
    private string oldDepth;
    private int oldTiles;
    private NCLR paleta;
    private PictureBox pic;
    private int startTile;
    private Button button1;
    private OpenFileDialog openFileDialog1;
    public NCGR tile;

    // Methods
    public iNCGR()
    {
        this.components = null;
        this.InitializeComponent();
    }
    public event EventHandler tilechange;
    public iNCGR(NCGR tile, NCLR paleta)
    {
        this.components = null;
        this.InitializeComponent();
        this.paleta = paleta;
        this.tile = tile;
        this.pic.Image = Imagen_NCGR.Crear_Imagen(tile, paleta, 0);
        this.numericWidth.Value = this.pic.Image.Width;
        this.numericHeight.Value = this.pic.Image.Height;
        this.comboDepth.Text = (tile.rahc.depth == ColorDepth.Depth4Bit) ? "4 bpp" : "8 bpp";
        this.oldDepth = this.comboDepth.Text;
        switch (tile.orden)
        {
            case Orden_Tiles.No_Tiles:
                this.oldTiles = 0;
                this.comboBox1.SelectedIndex = 0;
                break;

            case Orden_Tiles.Horizontal:
                this.oldTiles = 1;
                this.comboBox1.SelectedIndex = 1;
                break;

            case Orden_Tiles.Vertical:
                this.oldTiles = 2;
                this.comboBox1.SelectedIndex = 2;
                break;
        }
        this.comboDepth.SelectedIndexChanged += new EventHandler(this.comboDepth_SelectedIndexChanged);
        this.numericWidth.ValueChanged += new EventHandler(this.numericSize_ValueChanged);
        this.numericHeight.ValueChanged += new EventHandler(this.numericSize_ValueChanged);
        this.numericStart.ValueChanged += new EventHandler(this.numericStart_ValueChanged);
        this.Info();
        for (int i = 0; i < paleta.pltt.paletas.Length; i++)
        {
            this.comboBox2.Items.Add(i);
        }
    }

    private void Actualizar_Imagen()
    {
        if (this.tile.orden != Orden_Tiles.No_Tiles)
        {
            this.tile.rahc.nTilesX = (ushort) (this.numericWidth.Value / 8M);
            this.tile.rahc.nTilesY = (ushort) (this.numericHeight.Value / 8M);
        }
        else
        {
            this.tile.rahc.nTilesX = (ushort) this.numericWidth.Value;
            this.tile.rahc.nTilesY = (ushort) this.numericHeight.Value;
        }
        this.pic.Image = Imagen_NCGR.Crear_Imagen(this.tile, this.paleta, this.startTile);
    }

    private void btnSave_Click(object sender, EventArgs e)
    {
        SaveFileDialog dialog = new SaveFileDialog {
            AddExtension = true,
            DefaultExt = "bmp",
            Filter = "Imagen BitMaP (*.bmp)|*.bmp",
            OverwritePrompt = true
        };
        if (dialog.ShowDialog() == DialogResult.OK)
        {
            this.pic.Image.Save(dialog.FileName);
        }
        dialog.Dispose();
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.oldTiles != this.comboBox1.SelectedIndex)
        {
            switch (this.comboBox1.SelectedIndex)
            {
                case 0:
                    this.tile.orden = Orden_Tiles.No_Tiles;
                    this.tile.rahc.tileData.tiles[0] = Convertir.TilesToBytes(this.tile.rahc.tileData.tiles);
                    break;

                case 1:
                    this.tile.orden = Orden_Tiles.Horizontal;
                    this.tile.rahc.tileData.tiles = Convertir.BytesToTiles(this.tile.rahc.tileData.tiles[0]);
                    break;

                case 2:
                    this.tile.orden = Orden_Tiles.Vertical;
                    break;
            }
            this.oldTiles = this.comboBox1.SelectedIndex;
            this.Actualizar_Imagen();
        }
    }

    private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
    }

    private void comboBox2_SelectedIndexChanged_1(object sender, EventArgs e)
    {
        NCLR paleta = new NCLR();
        paleta = this.paleta;
        paleta.pltt.paletas[0] = this.paleta.pltt.paletas[this.comboBox2.SelectedIndex];
        if (this.comboBox2.SelectedIndex == 0)
        {
            this.pic.Image = Imagen_NCGR.Crear_Imagen(this.tile, this.paleta, 0);
        }
        else
        {
            this.pic.Image = Imagen_NCGR.Crear_Imagen(this.tile, paleta);
        }
    }

    private void comboDepth_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.comboDepth.Text != this.oldDepth)
        {
            byte[] buffer;
            this.oldDepth = this.comboDepth.Text;
            this.tile.rahc.depth = (this.comboDepth.Text == "4 bpp") ? ColorDepth.Depth4Bit : ColorDepth.Depth8Bit;
            if (this.comboDepth.Text == "4 bpp")
            {
                buffer = Convertir.Bit8ToBit4(Convertir.TilesToBytes(this.tile.rahc.tileData.tiles));
                this.tile.rahc.tileData.tiles = Convertir.BytesToTiles(buffer);
            }
            else
            {
                buffer = Convertir.Bit4ToBit8(Convertir.TilesToBytes(this.tile.rahc.tileData.tiles));
                this.tile.rahc.tileData.tiles = Convertir.BytesToTiles(buffer);
            }
            this.Actualizar_Imagen();
        }
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing && (this.components != null))
        {
            this.components.Dispose();
        }
        base.Dispose(disposing);
    }

    private void iNCGR_Load(object sender, EventArgs e)
    {
    }

    private void Info()
    {
        this.listInfo.Items[0].SubItems.Add("0x" + string.Format("{0:X}", this.tile.cabecera.constant));
        this.listInfo.Items[1].SubItems.Add(this.tile.cabecera.nSection.ToString());
        this.listInfo.Items[2].SubItems.Add(new string(this.tile.rahc.id));
        this.listInfo.Items[3].SubItems.Add("0x" + string.Format("{0:X}", this.tile.rahc.size_section));
        this.listInfo.Items[4].SubItems.Add(string.Concat(new object[] { this.tile.rahc.nTilesY.ToString(), " (0x", string.Format("{0:X}", this.tile.rahc.nTilesY), ')' }));
        this.listInfo.Items[5].SubItems.Add(string.Concat(new object[] { this.tile.rahc.nTilesX.ToString(), " (0x", string.Format("{0:X}", this.tile.rahc.nTilesX), ')' }));
        this.listInfo.Items[6].SubItems.Add(Enum.GetName(this.tile.rahc.depth.GetType(), this.tile.rahc.depth));
        this.listInfo.Items[7].SubItems.Add("0x" + string.Format("{0:X}", this.tile.rahc.unknown1));
        //this.listInfo.Items[8].SubItems.Add("0x" + string.Format("{0:X}", this.tile.rahc.padding));
        this.listInfo.Items[9].SubItems.Add("0x" + string.Format("{0:X}", this.tile.rahc.size_tiledata));
        this.listInfo.Items[10].SubItems.Add("0x" + string.Format("{0:X}", this.tile.rahc.unknown3));
    }

    private void InitializeComponent()
    {
        System.Windows.Forms.ListViewItem listViewItem12 = new System.Windows.Forms.ListViewItem(new string[] {
            "0x06",
            "Desconocido"}, -1);
        System.Windows.Forms.ListViewItem listViewItem13 = new System.Windows.Forms.ListViewItem(new string[] {
            "0x0E",
            "Nº secciones"}, -1);
        System.Windows.Forms.ListViewItem listViewItem14 = new System.Windows.Forms.ListViewItem(new string[] {
            "0x10",
            "ID Sección 1"}, -1);
        System.Windows.Forms.ListViewItem listViewItem15 = new System.Windows.Forms.ListViewItem(new string[] {
            "0x14",
            "Tamaño"}, -1);
        System.Windows.Forms.ListViewItem listViewItem16 = new System.Windows.Forms.ListViewItem(new string[] {
            "0x18",
            "Tiles Y"}, -1);
        System.Windows.Forms.ListViewItem listViewItem17 = new System.Windows.Forms.ListViewItem(new string[] {
            "0x1A",
            "Tiles X"}, -1);
        System.Windows.Forms.ListViewItem listViewItem18 = new System.Windows.Forms.ListViewItem(new string[] {
            "0x1C",
            "Formato"}, -1);
        System.Windows.Forms.ListViewItem listViewItem19 = new System.Windows.Forms.ListViewItem(new string[] {
            "0x20",
            "Desconocido"}, -1);
        System.Windows.Forms.ListViewItem listViewItem20 = new System.Windows.Forms.ListViewItem(new string[] {
            "0x24",
            "Desconocido"}, -1);
        System.Windows.Forms.ListViewItem listViewItem21 = new System.Windows.Forms.ListViewItem(new string[] {
            "0x28",
            "Tamaño píxels datos"}, -1);
        System.Windows.Forms.ListViewItem listViewItem22 = new System.Windows.Forms.ListViewItem(new string[] {
            "0x2C",
            "Desconocido"}, -1);
        this.numericWidth = new System.Windows.Forms.NumericUpDown();
        this.label1 = new System.Windows.Forms.Label();
        this.label2 = new System.Windows.Forms.Label();
        this.numericHeight = new System.Windows.Forms.NumericUpDown();
        this.groupProp = new System.Windows.Forms.GroupBox();
        this.label8 = new System.Windows.Forms.Label();
        this.comboBox2 = new System.Windows.Forms.ComboBox();
        this.label7 = new System.Windows.Forms.Label();
        this.label6 = new System.Windows.Forms.Label();
        this.comboBox1 = new System.Windows.Forms.ComboBox();
        this.label4 = new System.Windows.Forms.Label();
        this.btnSave = new System.Windows.Forms.Button();
        this.comboDepth = new System.Windows.Forms.ComboBox();
        this.label3 = new System.Windows.Forms.Label();
        this.numericStart = new System.Windows.Forms.NumericUpDown();
        this.listInfo = new System.Windows.Forms.ListView();
        this.columnPos = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
        this.columnCampo = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
        this.columnValor = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
        this.pic = new System.Windows.Forms.PictureBox();
        this.label5 = new System.Windows.Forms.Label();
        this.button1 = new System.Windows.Forms.Button();
        this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
        ((System.ComponentModel.ISupportInitialize)(this.numericWidth)).BeginInit();
        ((System.ComponentModel.ISupportInitialize)(this.numericHeight)).BeginInit();
        this.groupProp.SuspendLayout();
        ((System.ComponentModel.ISupportInitialize)(this.numericStart)).BeginInit();
        ((System.ComponentModel.ISupportInitialize)(this.pic)).BeginInit();
        this.SuspendLayout();
        // 
        // numericWidth
        // 
        this.numericWidth.Increment = new decimal(new int[] {
            1024,
            0,
            0,
            0});
        this.numericWidth.Location = new System.Drawing.Point(50, 269);
        this.numericWidth.Maximum = new decimal(new int[] {
            1024,
            0,
            0,
            0});
        this.numericWidth.Minimum = new decimal(new int[] {
            0,
            0,
            0,
            0});
        this.numericWidth.Name = "numericWidth";
        this.numericWidth.Size = new System.Drawing.Size(55, 20);
        this.numericWidth.TabIndex = 1;
        this.numericWidth.Value = new decimal(new int[] {
            1024,
            0,
            0,
            0});
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(7, 271);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(41, 13);
        this.label1.TabIndex = 2;
        this.label1.Text = "Ancho:";
        // 
        // label2
        // 
        this.label2.AutoSize = true;
        this.label2.Location = new System.Drawing.Point(148, 271);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(28, 13);
        this.label2.TabIndex = 3;
        this.label2.Text = "Alto:";
        // 
        // numericHeight
        // 
        this.numericHeight.Increment = new decimal(new int[] {
            1024,
            0,
            0,
            0});
        this.numericHeight.Location = new System.Drawing.Point(182, 269);
        this.numericHeight.Maximum = new decimal(new int[] {
            1024,
            0,
            0,
            0});
        this.numericHeight.Minimum = new decimal(new int[] {
            0,
            0,
            0,
            0});
        this.numericHeight.Name = "numericHeight";
        this.numericHeight.Size = new System.Drawing.Size(55, 20);
        this.numericHeight.TabIndex = 4;
        this.numericHeight.Value = new decimal(new int[] {
            1024,
            0,
            0,
            0});
        // 
        // groupProp
        // 
        this.groupProp.Controls.Add(this.button1);
        this.groupProp.Controls.Add(this.label8);
        this.groupProp.Controls.Add(this.comboBox2);
        this.groupProp.Controls.Add(this.label7);
        this.groupProp.Controls.Add(this.label6);
        this.groupProp.Controls.Add(this.comboBox1);
        this.groupProp.Controls.Add(this.label4);
        this.groupProp.Controls.Add(this.btnSave);
        this.groupProp.Controls.Add(this.comboDepth);
        this.groupProp.Controls.Add(this.label3);
        this.groupProp.Controls.Add(this.numericStart);
        this.groupProp.Controls.Add(this.listInfo);
        this.groupProp.Controls.Add(this.numericHeight);
        this.groupProp.Controls.Add(this.numericWidth);
        this.groupProp.Controls.Add(this.label2);
        this.groupProp.Controls.Add(this.label1);
        this.groupProp.Location = new System.Drawing.Point(269, 0);
        this.groupProp.Name = "groupProp";
        this.groupProp.Size = new System.Drawing.Size(243, 512);
        this.groupProp.TabIndex = 5;
        this.groupProp.TabStop = false;
        this.groupProp.Text = "Propiedades";
        // 
        // label8
        // 
        this.label8.AutoSize = true;
        this.label8.Location = new System.Drawing.Point(30, 320);
        this.label8.Name = "label8";
        this.label8.Size = new System.Drawing.Size(79, 13);
        this.label8.TabIndex = 15;
        this.label8.Text = "Pallete Number";
        // 
        // comboBox2
        // 
        this.comboBox2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
        this.comboBox2.FormattingEnabled = true;
        this.comboBox2.Location = new System.Drawing.Point(115, 317);
        this.comboBox2.Name = "comboBox2";
        this.comboBox2.Size = new System.Drawing.Size(121, 21);
        this.comboBox2.TabIndex = 14;
        this.comboBox2.SelectedIndexChanged += new System.EventHandler(this.comboBox2_SelectedIndexChanged_1);
        // 
        // label7
        // 
        this.label7.AutoSize = true;
        this.label7.Location = new System.Drawing.Point(37, 325);
        this.label7.Name = "label7";
        this.label7.Size = new System.Drawing.Size(0, 13);
        this.label7.TabIndex = 13;
        // 
        // label6
        // 
        this.label6.AutoSize = true;
        this.label6.Location = new System.Drawing.Point(37, 298);
        this.label6.Name = "label6";
        this.label6.Size = new System.Drawing.Size(72, 13);
        this.label6.TabIndex = 11;
        this.label6.Text = "Orden de tiles";
        // 
        // comboBox1
        // 
        this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
        this.comboBox1.FormattingEnabled = true;
        this.comboBox1.Items.AddRange(new object[] {
            "Sin tiles",
            "Horizontal",
            "Vertical"});
        this.comboBox1.Location = new System.Drawing.Point(115, 295);
        this.comboBox1.Name = "comboBox1";
        this.comboBox1.Size = new System.Drawing.Size(121, 21);
        this.comboBox1.TabIndex = 10;
        this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
        // 
        // label4
        // 
        this.label4.AutoSize = true;
        this.label4.Location = new System.Drawing.Point(148, 245);
        this.label4.Name = "label4";
        this.label4.Size = new System.Drawing.Size(28, 13);
        this.label4.TabIndex = 9;
        this.label4.Text = "bpp:";
        // 
        // btnSave
        // 
        this.btnSave.Image = global::MKDS_Course_Editor.Properties.Resources.picture_save;
        this.btnSave.Location = new System.Drawing.Point(9, 474);
        this.btnSave.Name = "btnSave";
        this.btnSave.Size = new System.Drawing.Size(79, 32);
        this.btnSave.TabIndex = 6;
        this.btnSave.Text = "Guardar";
        this.btnSave.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText;
        this.btnSave.UseVisualStyleBackColor = true;
        this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
        // 
        // comboDepth
        // 
        this.comboDepth.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
        this.comboDepth.FormattingEnabled = true;
        this.comboDepth.Items.AddRange(new object[] {
            "4 bpp",
            "8 bpp"});
        this.comboDepth.Location = new System.Drawing.Point(182, 242);
        this.comboDepth.Name = "comboDepth";
        this.comboDepth.Size = new System.Drawing.Size(54, 21);
        this.comboDepth.TabIndex = 8;
        // 
        // label3
        // 
        this.label3.AutoSize = true;
        this.label3.Location = new System.Drawing.Point(7, 245);
        this.label3.Name = "label3";
        this.label3.Size = new System.Drawing.Size(35, 13);
        this.label3.TabIndex = 7;
        this.label3.Text = "Inicio:";
        // 
        // numericStart
        // 
        this.numericStart.Location = new System.Drawing.Point(50, 243);
        this.numericStart.Maximum = new decimal(new int[] {
            1024,
            0,
            0,
            0});
        this.numericStart.Name = "numericStart";
        this.numericStart.Size = new System.Drawing.Size(55, 20);
        this.numericStart.TabIndex = 6;
        // 
        // listInfo
        // 
        this.listInfo.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.columnPos,
            this.columnCampo,
            this.columnValor});
        this.listInfo.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
        this.listInfo.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
            listViewItem12,
            listViewItem13,
            listViewItem14,
            listViewItem15,
            listViewItem16,
            listViewItem17,
            listViewItem18,
            listViewItem19,
            listViewItem20,
            listViewItem21,
            listViewItem22});
        this.listInfo.Location = new System.Drawing.Point(7, 20);
        this.listInfo.Name = "listInfo";
        this.listInfo.Size = new System.Drawing.Size(229, 216);
        this.listInfo.TabIndex = 5;
        this.listInfo.UseCompatibleStateImageBehavior = false;
        this.listInfo.View = System.Windows.Forms.View.Details;
        // 
        // columnPos
        // 
        this.columnPos.Text = "Posición";
        this.columnPos.Width = 54;
        // 
        // columnCampo
        // 
        this.columnCampo.Text = "Campo";
        this.columnCampo.Width = 88;
        // 
        // columnValor
        // 
        this.columnValor.Text = "Valor";
        this.columnValor.Width = 81;
        // 
        // pic
        // 
        this.pic.BackColor = System.Drawing.Color.Transparent;
        this.pic.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        this.pic.Location = new System.Drawing.Point(0, 0);
        this.pic.MaximumSize = new System.Drawing.Size(260, 480);
        this.pic.Name = "pic";
        this.pic.Size = new System.Drawing.Size(100, 50);
        this.pic.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
        this.pic.TabIndex = 0;
        this.pic.TabStop = false;
        this.pic.DoubleClick += new System.EventHandler(this.pic_DoubleClick);
        // 
        // label5
        // 
        this.label5.AutoSize = true;
        this.label5.Location = new System.Drawing.Point(3, 480);
        this.label5.Name = "label5";
        this.label5.Size = new System.Drawing.Size(209, 26);
        this.label5.TabIndex = 6;
        this.label5.Text = "Haga doble click en la imagen para verla a\r\ntamaño real.";
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(13, 341);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(75, 23);
        this.button1.TabIndex = 16;
        this.button1.Text = "Import";
        this.button1.UseVisualStyleBackColor = true;
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // openFileDialog1
        // 
        this.openFileDialog1.FileName = "openFileDialog1";
        // 
        // iNCGR
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.AutoScroll = true;
        this.BackColor = System.Drawing.Color.Transparent;
        this.Controls.Add(this.label5);
        this.Controls.Add(this.pic);
        this.Controls.Add(this.groupProp);
        this.Name = "iNCGR";
        this.Size = new System.Drawing.Size(512, 512);
        this.Load += new System.EventHandler(this.iNCGR_Load);
        ((System.ComponentModel.ISupportInitialize)(this.numericWidth)).EndInit();
        ((System.ComponentModel.ISupportInitialize)(this.numericHeight)).EndInit();
        this.groupProp.ResumeLayout(false);
        this.groupProp.PerformLayout();
        ((System.ComponentModel.ISupportInitialize)(this.numericStart)).EndInit();
        ((System.ComponentModel.ISupportInitialize)(this.pic)).EndInit();
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    private void numericSize_ValueChanged(object sender, EventArgs e)
    {
        this.Actualizar_Imagen();
    }

    private void numericStart_ValueChanged(object sender, EventArgs e)
    {
        this.startTile = (int) this.numericStart.Value;
        this.pic.Image = Imagen_NCGR.Crear_Imagen(this.tile, this.paleta, this.startTile);
    }

    private void pic_DoubleClick(object sender, EventArgs e)
    {
        Form form = new Form();
        PictureBox box = new PictureBox {
            Image = this.pic.Image,
            SizeMode = PictureBoxSizeMode.AutoSize
        };
        form.Controls.Add(box);
        form.BackColor = SystemColors.GradientInactiveCaption;
        form.Text = "Imagen a tama\x00f1o real";
        form.AutoScroll = true;
        form.MaximumSize = new Size(0x400, 0x300);
        form.ShowIcon = false;
        form.AutoSize = true;
        form.AutoSizeMode = AutoSizeMode.GrowAndShrink;
        form.MaximizeBox = false;
        form.Show();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (openFileDialog1.ShowDialog() == DialogResult.OK
            && openFileDialog1.FileName.Length > 0)
        {
            tile = Imagen_NCGR.BitmapToTile(openFileDialog1.FileName, tile.orden);
            if (tilechange != null)
            {
                tilechange(this, new EventArgs());
            }
        }
    }
}}

 

 
